package org.ah.ssm.ahutils;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ah.ssm.ahutils.AhConst.TAG;
import org.ah.ssm.entity.Question;
import org.ah.ssm.service.impl.QuestionServiceImpl;
import org.apache.log4j.Logger;

public class AhUtils {
	//public static Logger log4j = Logger.getLogger(AhUtils.class.getName());
	public static List<Question> newQs = new ArrayList<Question>();
	// <姓名，分数>
	public static Map<String, String> whoAnswered =new HashMap<String, String>();

	/**
	 * 获取JavaWeb项目运行的路径(src下的文件在此)
	 * 
	 * @return
	 */
	public static String ahGetProjPath() {
		Thread t = Thread.currentThread();
		ClassLoader cl = t.getContextClassLoader();
		// url:file:/D:/tools/Workspaces/.metadata/.me_tcat7/webapps/AhExam4SSM/WEB-INF/classes/
		URL url = cl.getResource("/");
		//
		return url.getPath().replace("%20", " ");
	}

	public static boolean ahIsNullOrEmpty(String s) {
		if (s == null || s.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 写log文件,同时打印到控制台
	 * 
	 * @param content
	 */
	public static void ahPrintLog(String content) {
		System.out.println(content);
		File f = new File(AhConst.LOG_FILE);
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			if (!f.exists()) {
				f.createNewFile();
			}
			// true追加
			fw = new FileWriter(f.getAbsoluteFile(), true);
			bw = new BufferedWriter(fw);
			bw.write(content);
			bw.newLine();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读文件取题
	 * 
	 * @param path
	 * @return
	 */
	public static ArrayList<Question> ahGetQsFromFile(String fileName) {
		ArrayList<Question> lst = new ArrayList<Question>();
		AhConst.TAG _tag = AhConst.TAG.TITLE;// 记录是标题,还是ABCD还是其他
		// 读文件
		try {
			FileInputStream _fis = new FileInputStream(ahGetProjPath() + "\\"
					+ fileName);
			InputStreamReader _ir = new InputStreamReader(_fis, "UTF-8");
			BufferedReader _br = new BufferedReader(_ir);

			Question q = new Question();
			String sLine = "";

			String sFirst = "";
			String sSecond = "";
			while ((sLine = _br.readLine()) != null) {
				if (sLine.length() <= 2) {
					sFirst = "";
					sSecond = "";
				} else {
					sFirst = sLine.substring(0, 1);
					sSecond = sLine.substring(1, 2);
				}

				if ("#".equals(sFirst) && !"#".equals(sSecond)) {
					// 标题(new 一道题)
					_tag = AhConst.TAG.TITLE;
					q = new Question();
					// q.setTitle(sLine.substring(1 + 2));
					int 点的位置 = sLine.indexOf(".");
					String sTitle = sLine.substring(点的位置 + 1);
					q.setTitle(sTitle);
				} else if ("#".equals(sFirst) && "#".equals(sSecond)) {
					_tag = AhConst.TAG.ANSWER;
					q.setAnswer(sLine.substring(2));
					lst.add(q);
				} else if ("A".equals(sFirst) && " ".equals(sSecond)) {
					_tag = AhConst.TAG.OPTA;
					q.setOptA(sLine);
				} else if ("B".equals(sFirst) && " ".equals(sSecond)) {
					_tag = AhConst.TAG.OPTB;
					q.setOptB(sLine);
				} else if ("C".equals(sFirst) && " ".equals(sSecond)) {
					_tag = AhConst.TAG.OPTC;
					q.setOptC(sLine);
				} else if ("D".equals(sFirst) && " ".equals(sSecond)) {
					_tag = AhConst.TAG.OPTD;
					q.setOptD(sLine);
				} else {
					if (_tag == AhConst.TAG.TITLE) {
						q.setTitle(q.getTitle() + AhConst.NEXT_LINE + sLine);
					} else if (_tag == AhConst.TAG.OPTA) {
						q.setOptA(q.getOptA() + AhConst.NEXT_LINE + sLine);
					} else if (_tag == AhConst.TAG.OPTB) {
						q.setOptB(q.getOptB() + AhConst.NEXT_LINE + sLine);
					} else if (_tag == AhConst.TAG.OPTC) {
						q.setOptC(q.getOptC() + AhConst.NEXT_LINE + sLine);
					} else if (_tag == AhConst.TAG.OPTD) {
						q.setOptD(q.getOptD() + AhConst.NEXT_LINE + sLine);
					}

				}
			}
			_br.close();
			_ir.close();
			_fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lst;
	}
	public static void ahWriteQs(){
		// 把题目记录下来
		int i = 1;
		for(Question q : AhUtils.newQs){
			AhUtils.ahPrintLog(i + q.getTitle());
			AhUtils.ahPrintLog("A." + q.getOptA());
			AhUtils.ahPrintLog("B." + q.getOptB());
			AhUtils.ahPrintLog("C." + q.getOptC());
			AhUtils.ahPrintLog("D." + q.getOptD());
			AhUtils.ahPrintLog("参考答案:" + q.getAnswer());
		}
	}
}
